/*
 * Copyright (C) 2025 Tsingteng MicroSystem
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef _NCI_TYPES_H_
#define _NCI_TYPES_H_

#include "stdbool.h"
#include <stdint.h>
#include "nci_defs.h"

/**
 * @file nci_types.h
 * @brief 定义NCI数据结构体
*/

#define NCI_DEFAULT_WAIT_TIME 2000

#define NCI_MAX_STD_REG_ID 0xFF
#define NCI_EXT_REG_PREFIX 0xA0

typedef enum {
    NCI_CORE_RESET_CMD = 0x2000,
    NCI_CORE_RESET_RSP = 0x4000,
    NCI_CORE_RESET_NTF = 0x6000,
    NCI_CORE_INIT_CMD = 0x2001,
    NCI_CORE_INIT_RSP = 0x4001,
    NCI_CORE_SET_CONFIG_CMD = 0x2002,
    NCI_CORE_SET_CONFIG_RSP = 0x4002,
    NCI_CORE_GET_CONFIG_CMD = 0x2003,
    NCI_CORE_GET_CONFIG_RSP = 0x4003,
    NCI_CORE_CONN_CREATE_CMD = 0x2004,
    NCI_CORE_CONN_CREATE_RSP = 0x4004,
    NCI_CORE_CONN_CLOSE_CMD = 0x2005,
    NCI_CORE_CONN_CLOSE_RSP = 0x4005,
    NCI_CORE_CONN_CREDITS_NTF = 0x6006,
    NCI_CORE_GENERIC_ERROR_NTF = 0x6007,
    NCI_CORE_INTERFACE_ERROR_NTF = 0x6008,
    NCI_CORE_SET_POWER_SUB_STATE_CMD = 0x2009,
    NCI_CORE_SET_POWER_SUB_STATE_RSP = 0x4009,

    NCI_RF_DISCOVER_MAP_CMD = 0x2100,
    NCI_RF_DISCOVER_MAP_RSP = 0x4100,
    NCI_RF_SET_LISTEN_MODE_ROUTING_CMD = 0x2101,
    NCI_RF_SET_LISTEN_MODE_ROUTING_RSP = 0x4101,
    NCI_RF_GET_LISTEN_MODE_ROUTING_CMD = 0x2102,
    NCI_RF_GET_LISTEN_MODE_ROUTING_RSP = 0x4102,
    NCI_RF_GET_LISTEN_MODE_ROUTING_NTF = 0x6102,
    NCI_RF_DISCOVER_CMD = 0x2103,
    NCI_RF_DISCOVER_RSP = 0x4103,
    NCI_RF_DISCOVER_NTF = 0x6103,
    NCI_RF_DISCOVER_SELECT_CMD = 0x2104,
    NCI_RF_DISCOVER_SELECT_RSP = 0x4104,
    NCI_RF_INTF_ACTIVATED_NTF = 0x6105,
    NCI_RF_DEACTIVATE_CMD = 0x2106,
    NCI_RF_DEACTIVATE_RSP = 0x4106,
    NCI_RF_DEACTIVATE_NTF = 0x6106,
    NCI_RF_FIELD_INFO_NTF = 0x6107,
    NCI_RF_T3T_POLLING_CMD = 0x2108,
    NCI_RF_T3T_POLLING_RSP = 0x4108,
    NCI_RF_T3T_POLLING_NTF = 0x6108,
    NCI_RF_NFCEE_ACTION_NTF = 0x6109,
    NCI_RF_NFCEE_DISCOVERY_REQ_NTF = 0x610A,
    NCI_RF_ISO_DEP_NAK_PRESENCE_CMD = 0x2110,
    NCI_RF_ISO_DEP_NAK_PRESENCE_RSP = 0x4110,
    NCI_RF_ISO_DEP_NAK_PRESENCE_NTF = 0x6110,

    NCI_NFCEE_DISCOVER_CMD = 0x2200,
    NCI_NFCEE_DISCOVER_RSP = 0x4200,
    NCI_NFCEE_DISCOVER_NTF = 0x6200,
    NCI_NFCEE_MODE_SET_CMD = 0x2201,
    NCI_NFCEE_MODE_SET_RSP = 0x4201,
    NCI_NFCEE_MODE_SET_NTF = 0x6201,
    NCI_NFCEE_STATUS_NTF = 0x6202,
    NCI_NFCEE_POWER_AND_LINK_CNTRL_CMD = 0x2203,
    NCI_NFCEE_POWER_AND_LINK_CNTRL_RSP = 0x4203,
} tNCI_GID_OID;

typedef enum {
    NCI_DATA = 0b000,
    NCI_CMD = 0b001,
    NCI_RSP = 0b010,
    NCI_NTF = 0b011,
} tNCI_MT;

typedef enum {
    NCI_CORE = 0b0000,
    NCI_RF = 0b0001,
    NCI_NFCEE = 0b0010,
    NCI_PROPRIETARY = 0b1111,
} tNCI_GID;

typedef enum {
    NCI_CORE_RESET = 0x00,
    NCI_CORE_INIT  = 0x01,
    NCI_CORE_SET_CONFIG = 0x02,
    NCI_CORE_GET_CONFIG = 0x03,
    NCI_CORE_CONN_CREATE = 0x04,
    NCI_CORE_CONN_CLOSE = 0x05,
    NCI_CORE_CONN_CREDITS = 0x06,
    NCI_CORE_GENERIC_ERROR = 0x07,
    NCI_CORE_INTERFACE_ERROR = 0x08,
    NCI_CORE_SET_POWER_SUB_STATE = 0x09,
} tNCI_CORE_OID;

typedef enum {
    NCI_RF_DISCOVER_MAP = 0x00,
    NCI_RF_SET_LISTEN_MODE_ROUTING = 0x01,
    NCI_RF_GET_LISTEN_MODE_ROUTING = 0x02,
    NCI_RF_DISCOVER = 0x03,
    NCI_RF_DISCOVER_SELECT = 0x04,
    NCI_RF_INTF_ACTIVATED = 0x05,
    NCI_RF_DEACTIVATE = 0x06,
    NCI_RF_FIELD_INFO = 0x07,
    NCI_RF_T3T_POLLING = 0x08,
    NCI_RF_NFCEE_ACTION = 0x09,
    NCI_RF_NFCEE_DISCOVERY_REQ = 0x0A,
    NCI_RF_PARAMETER_UPDATE = 0x0B,
    NCI_RF_INTF_EXT_START = 0x0C,
    NCI_RF_INTF_EXT_STOP = 0x0D,
    NCI_RF_EXT_AGG_ABORT = 0x0E,
    NCI_RF_NDEF_ABORT = 0x0F,
    NCI_RF_ISO_DEP_NAK_PRESENCE = 0x10,
    NCI_RF_SET_FORCED_NFCEE_ROUTING = 0x11,
} tNCI_RF_OID;

typedef enum {
    NCI_NFCEE_DISCOVER = 0x00,
    NCI_NFCEE_MODE_SET = 0x01,
    NCI_NFCEE_STATUS = 0x02,
    NCI_NFCEE_POWER_AND_LINK_CNTRL = 0x03,
} tNCI_NFCEE_OID;

typedef enum {
    NCI_PROPRIETARY_M1_AUTH_STATUS = 0x30,
    NCI_PROPRIETARY_DEBUG_L1 = 0x35,
    NCI_PROPRIETARY_DEBUG_L2 = 0x36,
    NCI_PROPRIETARY_DEBUG_L3 = 0x41,
} tNCI_PTY_OID;

typedef enum {
    NCI_RF_TECH_A_PASSIVE_POLL_MODE = 0x00,
    NCI_RF_TECH_B_PASSIVE_POLL_MODE = 0x01,
    NCI_RF_TECH_F_PASSIVE_POLL_MODE = 0x02,
    NCI_RF_TECH_ACTIVE_POLL_MODE = 0x03,
    NCI_RF_TECH_V_PASSIVE_POLL_MODE = 0x06,
    NCI_RF_TECH_A_PASSIVE_LISTEN_MODE = 0x80,
    NCI_RF_TECH_B_PASSIVE_LISTEN_MODE = 0x81,
    NCI_RF_TECH_F_PASSIVE_LISTEN_MODE = 0x82,
    NCI_RF_TECH_ACTIVE_LISTEN_MODE = 0x83,
} tNCI_RF_TECH_MODE;

typedef enum {
    NCI_BIT_RATE_106 = 0x00,
    NCI_BIT_RATE_212 = 0x01,
    NCI_BIT_RATE_424 = 0x02,
    NCI_BIT_RATE_848 = 0x03,
    NCI_BIT_RATE_1695 = 0x04,
    NCI_BIT_RATE_3390 = 0x05,
    NCI_BIT_RATE_6780 = 0x06,
    NCI_BIT_RATE_26 = 0x20,
} tNCI_BIT_RATES;

typedef enum {
    NCI_RF_INTERFACE_EXTENSIONS_FRAME_AGGREGATED = 0x00,
    NCI_RF_INTERFACE_EXTENSIONS_LLCP_SYMMETRY = 0x01,
} tNCI_RF_INTERFACE_EXTENSIONS;

typedef enum {
    NCI_CHANNEL_DESTINATION_TYPE_NFCC_LOOPBACK = 0x01,
    NCI_CHANNEL_DESTINATION_TYPE_REMOTE_NFC_ENDPOINT = 0x02,
    NCI_CHANNEL_DESTINATION_TYPE_NFCEE = 0x03,
} tNCI_CHANNEL_DESTINATION_TYPE;

// NCI CORE TYPE Define

typedef enum {
    KEEP_CONFIG = 0x00,
    RESET_CONFIG = 0x01,
} tNCI_RESET_TYPE;

typedef enum {
    NCI_RESET_TRIGGER_UNRECOVERABLE_ERROR_OCCURRED = 0x00,
    NCI_RESET_TRIGGER_NFCC_WAS_POWERED_ON = 0x01,
    NCI_RESET_TRIGGER_CORE_RESET_CMD_WAS_RECEIVED = 0x02,
} tNCI_RESET_TRIGGER;

typedef enum {
    NCI_RESET_RF_CONFIGURATION_HAS_BEEN_KEPT = 0x00,
    NCI_RESET_RF_CONFIGURATION_HAS_BEEN_RESET = 0x01,
} tNCI_CONFIGURATION_STATUS;

#define MAX_NFCC_MANUF_SPEC_INFO_LEN 10

typedef struct {
    tNCI_RESET_TRIGGER trigger;
    tNCI_CONFIGURATION_STATUS configuration_status;
    uint8_t nci_version;
    uint8_t manufacturer_id;
    uint8_t manufacturer_specific_infomation_length;
    uint8_t manufacturer_specific_infomation[MAX_NFCC_MANUF_SPEC_INFO_LEN];
} tNCI_CORE_RESET_NTF;

/** @brief CORE INIT RESPONSE */
typedef struct {
    uint32_t nfcc_feature;
    uint16_t max_routing_table_size;
    uint16_t max_nfcv_rf_frame_size;
    uint8_t max_logicl_conn;
    uint8_t max_ctrl_packet_payload_size;
    uint8_t max_hci_packet_payload_size;
    uint8_t hci_credits_number;
} tNCI_INIT_RESP;

#define NCI_MAX_REG_VALUE_LEN 256

typedef uint16_t tNCI_REG_ID;

typedef struct {
    tNCI_REG_ID id;
    uint8_t len;
    uint8_t *value;
} tNCI_REG;

typedef enum {
    NFCC_LOOPBACK = 0x01,
    REMOTE_NFC_ENDPOINT = 0x02,
    NFCEE = 0x03,
} tNCI_DESTINATION_TYPE;

typedef struct {
    uint8_t type;
    uint8_t len;
    uint16_t value;
} tNCI_DEST_SPEC_PARAM;

typedef struct {
    uint8_t max_data_packet_payload_size;
    uint8_t initial_num_credits;
    uint8_t conn_id;
} tNCI_DATA_CHANNEL_INFO;

typedef struct {
    tNCI_RESET_TRIGGER trigger;
    tNCI_CONFIGURATION_STATUS configuration_status;
    tNCI_VERSION nci_version;
    uint8_t manufacturer_id;
    uint8_t manufacturer_specific_infomation_length;
    uint8_t *manufacturer_specific_infomation;
} tNTF_CORE_RESET;

typedef struct {
    tNCI_RF_INTERFACE interface;
    uint8_t number_of_extensions;
    tNCI_RF_INTERFACE_EXTENSIONS* extensions;
} tNCI_SUPPORTED_RF_INTERFACES;

typedef uint8_t tNCI_CONN_ID;

#define NCI_STATIC_RF_CONN_ID 0b0000
#define NCI_STATIC_HCI_CONN_ID 0b0001
#define NCI_MAX_NUM_CONN 10

typedef struct {
    tNCI_CONN_ID conn_id;
    uint8_t credits;
} tNCI_CREDIT_ENTRY;

#define NCI_REG_NFCC_CONFIG_CONTROL 0x85
#define NCI_REG_TOTAL_DURATION 0x00
#define NCI_REG_CON_DEVICES_LIMIT 0x01
#define NCI_REG_CON_DISCOVERY_PARAM 0x02

/* The DH-NFCEE listen is considered as a enable NFCEE */
#define NCI_LISTEN_DH_NFCEE_ENABLE_MASK 0x00
/* The DH-NFCEE listen is considered as a disable NFCEE */
#define NCI_LISTEN_DH_NFCEE_DISABLE_MASK 0x02
/* The DH polling is considered as a disable NFCEE */
#define NCI_POLLING_DH_DISABLE_MASK 0x00
/* The DH polling is considered as a enable NFCEE */
#define NCI_POLLING_DH_ENABLE_MASK 0x01

// NCI RF

typedef struct {
    tNCI_RF_DISCOVERY_ID id;
    tNCI_RF_PROTOCOL protocol;
    tNCI_RF_TECH_MODE tech_mode;
    tNCI_RF_TECH_PARAMS tech_params;
    bool is_last;
} tNCI_RF_DISCOVER_PARAMS;

typedef struct {
    tNCI_RF_DISCOVERY_ID id;
    tNCI_RF_INTERFACE interface;
    tNCI_RF_PROTOCOL protocol;
    tNCI_RF_TECH_MODE activation_tech_mode;
    uint8_t max_data_packet_payload_size;
    uint8_t initial_number_of_credits;
    tNCI_RF_TECH_PARAMS tech_params;
    tNCI_RF_TECH_MODE data_exchange_tech_mode;
    uint8_t data_exchange_transmit_bit_rate;
    uint8_t data_exchange_receive_bit_rate;
    tNCI_RF_ACT_PARAMS act_params;
} tNCI_RF_INTF_ACTIVATED_PARAMS;

#define NCI_RF_ISO_DEP_MAX_DATA_PAYLOAD_SIZE 0xFD

#endif /* _NCI_TYPES_H_ */
